package com.mezz.bones.im.gateway.action.impl;


import cn.hutool.core.util.StrUtil;
import com.mezz.bones.im.common.enums.ActionCodeEnum;
import com.mezz.bones.im.common.message.base.MessageHeader;
import com.mezz.bones.im.common.message.body.in.NonReq;
import com.mezz.bones.im.common.domain.user.ImUserConnectInfo;
import com.mezz.bones.im.common.result.ImResult;
import com.mezz.bones.im.gateway.action.interfaces.AbstractSysMessageAction;
import com.mezz.bones.im.gateway.manager.UserConnectInfoManager;
import com.mezz.bones.im.gateway.util.ChannelUtil;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: zhuang
 * @Date: 2024/11/29 18:39
 * @Description: TODO
 * @Version: 1.0
 **/

@Service
public class LogoutSysAction extends AbstractSysMessageAction<NonReq> {

    @Resource
    private UserConnectInfoManager userConnectInfoManager;

    @Override
    public String tag() {
        return ActionCodeEnum.CLIENT_LOGOUT.getCode();
    }

    @Override
    public void process(MessageHeader header, NonReq req, ChannelHandlerContext ctx) {

        //强制关闭连接信息
        ImUserConnectInfo userInfo = userConnectInfoManager.getUserConnectInfo(ctx);
        //切换账号不需要关闭连接
        log.info("用户退出登录关闭连接----用户[{}]", userInfo);
        ChannelUtil.sendAndGracefullyClose(ctx.channel(), ImResult.errorMsg(header.getTraceId(),ActionCodeEnum.CLIENT_LOGOUT.getCode(), StrUtil.format("用户退出登录关闭连接----用户[{}]", userInfo)));
    }
}
